package com.aaa.car.controller;

import com.aaa.car.entity.TbMaintainShop;
import com.aaa.car.service.TbMaintainShopService;
import com.aaa.common.constant.ExceptionConstant;
import com.aaa.common.constant.ReturnCode;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.Result;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;

/**
 * (TbMaintainShop)表控制层
 *
 * @author makejava
 * @since 2020-11-18 23:03:31
 */
@RestController
@RequestMapping("tbMaintainShop")
public class TbMaintainShopController {
    /**
     * 服务对象
     */
    @Resource
    private TbMaintainShopService tbMaintainShopService;

    /**
     * 分页+查询维修厂
     * @param pageNo
     * @param pageSize
     * @param tbMaintainShop
     * @return
     */
    @GetMapping("queryAllMaintainShop")
    public Result queryAllMaintainShop(Integer pageNo, Integer pageSize, TbMaintainShop tbMaintainShop){
        if(pageNo==null||pageSize==null){
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(),
                    ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        }
        return tbMaintainShopService.queryAllMaintainShop(pageNo,pageSize,tbMaintainShop);
    }

    /**
     * 修改优惠券信息
     * @param tbMaintainShop
     * @return
     */
    @PutMapping("update")
    public Result update(@RequestBody TbMaintainShop tbMaintainShop){
        //当ID空为时抛出自定义异常
        if (tbMaintainShop.getId()==null) {
            throw new CustomException(ExceptionConstant.ID_NOT_NULL.getErrorCode(),
                    ExceptionConstant.ID_NOT_NULL.getErrorMessage());
        }
        //返回成功
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(), this.tbMaintainShopService.update(tbMaintainShop));
    }

    /**
     * 添加优惠券信息
     *
     * @param tbMaintainShop
     * @return
     */
    @PostMapping("insert")
    public Result inset(@RequestBody TbMaintainShop tbMaintainShop) {
        //判断新增的角色名字为空抛出异常
        if (tbMaintainShop.getName() == null) {
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(),
                    ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        } else {
            //返回成功
                return new Result(ReturnCode.SUCCESSS.getCode(),
                        ReturnCode.SUCCESSS.getMsg(), this.tbMaintainShopService.insert(tbMaintainShop));
        }
    }
    /**
     * 删除优惠券信息
     * @param id
     * @return
     */
    @DeleteMapping("deleteById")
    public Result deleteById(@Param("id") Integer id){
        //当ID空为时抛出自定义异常
        if (null == id) {
            throw new CustomException(ExceptionConstant.ID_NOT_NULL.getErrorCode(),
                    ExceptionConstant.ID_NOT_NULL.getErrorMessage());
        }
        //返回成功
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(), this.tbMaintainShopService.deleteById(id));
    }

}